package com.cxrj.pqm.pqmManagement.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxrj.pqm.pqmManagement.entity.PqmItemWithBLOBs;
/*import com.cxrj.pqm.pqmManagement.entity.PqmItemWithBLOBs;*/
import com.cxrj.pqm.pqmManagement.service.PqmService;

@Controller
public class PqmController {

	@Resource
	private PqmService pqmService;

	/**
	 * 添加或修改项目
	 * 
	 * @param pqmItemWithBLOBs
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "insertItem.do")
	@ResponseBody
	public Map<String, Object> insertAndUpdateItem(PqmItemWithBLOBs pqmItemWithBLOBs, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			map = pqmService.insertAndUpdateItem(pqmItemWithBLOBs, request);
		} catch (Exception e) {
			map.put("result", 0);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 查询数据
	 * 
	 * @param itemName
	 *            项目名
	 * @param index
	 *            起始位置
	 * @param size
	 *            每页条数
	 * @param type
	 *            项目类型
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param sorting
	 *            排序方式
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "selectItem.do")
	@ResponseBody
	public Map<String, Object> selectItem(String itemName, String index, String size, String type, String startTime, String endTime, String sorting, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			map = pqmService.selectItem(itemName, index, size, type, startTime, endTime, sorting);
		} catch (Exception e) {
			map.put("result", 0);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 查询详情
	 * 
	 * @param itemID
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "selectByItemId.do")
	@ResponseBody
	public Map<String, Object> selectByItemId(String itemID, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			map = pqmService.selectByItemID(itemID);
		} catch (Exception e) {
			map.put("result", 0);
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 根据userID查询
	 * 
	 * @param itemName
	 *            项目名
	 * @param index
	 *            起始位置
	 * @param size
	 *            每页条数
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @param sorting
	 *            排序方式
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "selectUserId.do")
	@ResponseBody
	public Map<String, Object> selectByUserID(String index, String size, String itemName, String startTime, String endTime, String sorting, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			String userID = request.getSession().getAttribute("openid") + "";
			if (request.getSession().getAttribute("openid") != null) {
				userID = request.getSession().getAttribute("openid").toString();
			}
			map = pqmService.selectByUserID(userID, index, size, itemName, startTime, endTime, sorting);
		} catch (Exception e) {
			map.put("result", 0);
			e.printStackTrace();
		}
		return map;
	}

}
